﻿using CmccTool.KPI.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static CmccTool.KPI.DbTables.KpiTables;
using SQLite;
namespace CmccTool.KPI.View
{
    public partial class FrmUpdateGc : Form
    {
        private readonly SQLiteConnection? _db = KpiImpHelper.KpiDbChh();

        public FrmUpdateGc(string CGI, string ENI)
        {
            InitializeComponent();

            txbCgi.Text = CGI;
            txbEni.Text = ENI;

            if (_db == null) { return; }

            GcCgiInfo info = _db.Table<GcCgiInfo>()
                           .FirstOrDefault(g => g.CGI == CGI);

            if (info != null)
            {
                txbCellName.Text = info.GcCellName;
                txbNodeName.Text = info.GcNodeName;

            }

        }

        public FrmUpdateGc()
        {
            InitializeComponent();
        }

        private bool IsValidInput()
        {



            foreach (Control control in this.tableLayoutPanel1.Controls)
            {

                if (control is TextBox textBox && string.IsNullOrWhiteSpace(textBox.Text))
                {
                    switch (control.Name)
                    {
                        case "txbCGI" or "txbConfirmPerson":
                            continue;
                        default:
                            MessageBox.Show($"请填写 {textBox.Name} 字段");
                            return false;
                    }
                }
                else if (control is ComboBox comboBox && string.IsNullOrWhiteSpace(comboBox.Text))
                {
                    MessageBox.Show($"请填写 {comboBox.Name} 字段");
                    return false;
                }

            }

            return true;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if(_db == null) {MessageBox.Show("数据库链接失败！"); return; } 

            if (IsValidInput())
            {
                string cgi = txbCgi.Text;
                string eni = txbEni.Text;
                string celName = txbCellName.Text;
                string nodeName = txbNodeName.Text;

                GcCgiInfo info = new GcCgiInfo
                {
                    CGI = cgi,
                    ENI = eni,
                    GcCellName = celName,
                    GcNodeName = nodeName
                };


                _db.InsertOrReplace(info);

                this.Close();
            }
        }
    }
}
